package cn.com.gridinfo.sooc.core.expand.callname.activity;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.jeremy.arad.utils.Log;
import com.marshalchen.ultimaterecyclerview.UltimateRecyclerView;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import cn.com.gridinfo.sooc.R;
import cn.com.gridinfo.sooc.base.BaseSoocActivity;
import cn.com.gridinfo.sooc.core.expand.callname.adapter.QueXiClassAdapter;
import cn.com.gridinfo.sooc.core.expand.callname.common.CodeConst;
import cn.com.gridinfo.sooc.core.expand.callname.common.PageConst;
import cn.com.gridinfo.sooc.core.expand.callname.dao.CallNameDao;
import cn.com.gridinfo.sooc.utils.MessageUtils;

/**
 * 缺席课堂界面
 *
 * @author yingchunli
 */
public class QuexiClassActivity extends BaseSoocActivity {


    @InjectView(R.id.studentName)
    TextView studentName;//学生姓名

    @InjectView(R.id.studentClass)
    TextView studentClass;//学生班级

    @InjectView(R.id.list_quexiclass)
    UltimateRecyclerView listQuexiclass;//列表

    @InjectView(R.id.count_quexiclass)
    TextView quexiCount;//缺席次数
    LinearLayoutManager linearLayoutManager;//布局管理器
    QueXiClassAdapter adapter;//适配器
    CallNameDao dao;
    int maxLastPositon;//数据最后位置
    String xh;//学号
    String kcid;//课程id
    String kcmc;//课程名称

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.expand_callname_quexiclass);
        ButterKnife.inject(this);
        dao = new CallNameDao(this);
        //初始化listView
        linearLayoutManager = new LinearLayoutManager(this);
        listQuexiclass.setLayoutManager(linearLayoutManager);

        xh = getIntent().getStringExtra("xh");
        kcid = getIntent().getStringExtra("kcid");
        kcmc = getIntent().getStringExtra("kcmc");

        //显示 姓名 学号  班级  和 缺席人数
        studentName.setText(getIntent().getStringExtra("xm") + "(" + getIntent().getStringExtra("xh") + ")");
        studentClass.setText(getIntent().getStringExtra("bj"));
        quexiCount.setText(getIntent().getStringExtra("qxcs"));

        PageConst.pageNo = 1;
        PageConst.pageSize = 10;
        //发送请求  获取列表
        dao.getquexiClass(xh, kcid, PageConst.pageNo, PageConst.pageSize, CodeConst.EXPAND_QUEXI_GET_CLASS_FRIST);
    }

    @Override
    public void onRequestSuccess(int requestCode) {
        try {
            if (dao.getStatus().equals("0")) {
                switch (requestCode) {
                    case CodeConst.EXPAND_QUEXI_GET_CLASS_FRIST:
                        //获取列表成功
                        initView();
                        Log.d("status---->" + dao.getStatus());
                        if (dao.getClassInfoList() != null && !dao.getClassInfoList().isEmpty()) {

                            listQuexiclass.hideEmptyView();
                        } else {
                            //无数据处理
                            listQuexiclass.showEmptyView();
                        }

                        break;
                    case CodeConst.EXPAND_QUEXI_GET_CLASS_PUSHDOWN:
                        listQuexiclass.setRefreshing(false);
                        if (dao.getClassInfoList() != null && !dao.getClassInfoList().isEmpty()) {
                            listQuexiclass.hideEmptyView();
                        } else {
                            listQuexiclass.showEmptyView(); //无数据处理
                        }
                        adapter.refresh(dao.getClassInfoList());
                        if (dao.getClassInfoList().size() > (PageConst.pageSize - 1)) {
                            adapter.loadMoreDataView();
                        } else {
                            //数据小于当前页的话则不显示加载更多
                            adapter.loadNoDataView();
                            linearLayoutManager.scrollToPositionWithOffset(maxLastPositon, -1);
                            linearLayoutManager.scrollToPosition(maxLastPositon);
                        }
                        break;
                    case CodeConst.EXPAND_QUEXI_GET_CLASS_PULLUP:
                        if (dao.getClassInfoList() != null && !dao.getClassInfoList().isEmpty()) {
                            adapter.loadmore(dao.getClassInfoList());
                        } else {
                            //无数据则不显示下一页
                            adapter.loadNoDataView();
                            linearLayoutManager.scrollToPositionWithOffset(maxLastPositon, -1);
                        }
                        break;
                }
            } else {
                MessageUtils.showImageShortToast(this, dao.getStatusCode());
            }
        } catch (Exception e) {
            MessageUtils.showImageShortToast(this, "请求数据异常");
        }
    }

    /**
     * 初始化列表
     */
    void initView() {
        adapter = new QueXiClassAdapter(dao.getClassInfoList(), this, kcmc);
        listQuexiclass.setAdapter(adapter);
        listQuexiclass.enableLoadmore();
        listQuexiclass.setDefaultOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                PageConst.pageNo = 1;//首页
                dao.getquexiClass(xh, kcid, PageConst.pageNo, PageConst.pageSize, CodeConst.EXPAND_QUEXI_GET_CLASS_PUSHDOWN);
                linearLayoutManager.scrollToPosition(0);
            }
        });
        listQuexiclass.setOnLoadMoreListener(new UltimateRecyclerView.OnLoadMoreListener() {
            @Override
            public void loadMore(final int itemsCount, final int maxLastVisiblePosition) {
                if (itemsCount > (PageConst.pageSize - 1)) {
                    maxLastPositon = maxLastVisiblePosition;
                    dao.getquexiClass(xh, kcid, ++PageConst.pageNo, PageConst.pageSize, CodeConst.EXPAND_QUEXI_GET_CLASS_PULLUP);
                } else {
                    adapter.loadNoDataView();
                    linearLayoutManager.scrollToPositionWithOffset(maxLastVisiblePosition, -1);
                }
            }
        });
    }

    @Override
    public String setupToolBarTitle() {
        return "缺席课堂";
    }

    @Override
    public boolean setupToolBarLeftButton(ImageView leftButton) {
        return true;
    }

    @OnClick(R.id.toolbar_leftbtn)
    void onToolbarLeftClick() {
        finish();
    }
}
